﻿using IOCDI.IBLL;
using IOCDI.IDAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace IOCDI.Factory
{
    public class ObjectFactory
    {
        public static IUserDAL CreateUserDAL()
        {
            IUserDAL userDAL = null!;
            string config=ConfigurationManager.GetNode("IUserDAL");
            //不能依赖细节,但是又要创建对象
            Assembly assembly = Assembly.LoadFrom(config.Split(',')[1]);
            Type type = assembly.GetType(config.Split(',')[0])!;
            userDAL = (IUserDAL)Activator.CreateInstance(type)!;
            return userDAL;
        }

        public static IUserBLL CreateUserBLL(IUserDAL userDAL)
        {
            IUserBLL userBLL = null!;
            string config = ConfigurationManager.GetNode("IUserBLL");
            //不能依赖细节,但是又要创建对象
            Assembly assembly = Assembly.LoadFrom(config.Split(',')[1]);
            Type type = assembly.GetType(config.Split(',')[0])!;
            userBLL = (IUserBLL)Activator.CreateInstance(type, new object[] { userDAL })!;//反射创建对象,并给构造函数传参数
            return userBLL;
        }
    }
}
